//
//  ByUIIndexVC.m
//  laserdevice_ios
//
//  Created by pro on 16/11/21.
//  Copyright © 2016年 com.itboye. All rights reserved.
//


#import "ByUIIndexVC.h"

#import "ByBlueToothHandleModel.h"

// vc
#import "ByBlueToothListVC.h"           // 蓝牙列表
#import "ByUISceneVC.h"                 // 场景测量
#import "ByUIRealTimeVC.h"              // 实时测量
#import "ByUIDataImportVC.h"            // 数据导入
#import "ByUISettingVC.h"               // 设置（设置语言和长度单位）
#import "ByUIHelpVC.h"                  // 帮助文档


#import "ByLogTestVC.h"

@interface ByUIIndexVC ()

@end

@implementation ByUIIndexVC

#pragma mark - vc method
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = STRING(@"shouye");
    
//    [self addBYItemviews:@[@"蓝牙日志"] mode:ByNavigationItemModeText position:ByNavigationItemPositionModeLeft];

    [self addBYItemviews:@[STRING(@"lanyalianji")] mode:ByNavigationItemModeImageAndText position:ByNavigationItemPositionModeRight];
    self.tintColor = [UIColor whiteColor];
    
    [self createViews];
}

#pragma mark - 创建视图
- (void)createViews {
    
    CGFloat x, y, width, height;
    CGFloat margin = 10;

    // 1.背景视图
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    bgImageView.contentMode = UIViewContentModeScaleToFill;
    bgImageView.image = [UIImage imageNamed:@"index_bg"];
    [self.view addSubview:bgImageView];
    
    // 2.实时测距，数据导入，设置，帮助文档
    NSArray *images = @[@"index_shishiceliang",
                        @"index_shujudaoru",
                        @"index_shezhi",
                        @"index_bangzhuwendang"];
    NSArray *texts = @[STRING(@"shishiceju"),
                       STRING(@"shujudaoru"),
                       STRING(@"shezhi"),
                       STRING(@"bangzhuwendang"),];
    for (int i = 0; i < texts.count; i ++) {
        width = (SCREEN_WIDTH - 3 * margin) / 2;
        height = width;
        x = margin + (i % 2) * (width + margin);
        y = 0;  // 需要另外适配，因为是从下往上排序的
        UIControl *view = [self createVerticalModeView:CGRectMake(x, y, width, height)
                                             imageSize:CGSizeMake(width * 0.4, width * 0.4)
                                             imageName:images[i]
                                              textFont:FONT(18)
                                                  text:texts[i]
                                                margin:20];
        view.layer.cornerRadius = 5;
        view.tag = 3 + i; // 3，4，5，6
        [self.view addSubview:view];
        // 设置纵轴位置
        view.bottom = SCREEN_HEIGHT - 64 - margin - (1 - i / 2) * (height + margin);
        
        [view addTarget:self action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    // 3.手机图片，文字（距离，由你掌控）
    x = 0;
    y = 0;
    width = self.view.width;
    height = (SCREEN_HEIGHT - 64 - SCREEN_WIDTH) / 3;
    UIControl *topView = [self createHorizontalModeView:CGRectMake(x, y, width, height)
                                              imageSize:CGSizeMake(height * 0.35, height * 0.7)
                                              imageName:@"index_shouji"
                                               textFont:FONT(25)
                                                   text:STRING(@"juli")
                                                 margin:30];
    [self.view addSubview:topView];
    topView.tag = 1;
    [topView addTarget:self action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];
    
    // 4.尺图片，文字（场景测量）
    x = margin;
    y = topView.bottom + margin;
    width = self.view.width - margin * 2;
    height = topView.height * 2 - margin;
    UIControl *midView = [self createHorizontalModeView:CGRectMake(x, y, width, height)
                                              imageSize:CGSizeMake(height * 0.5, height * 0.5)
                                              imageName:@"index_changjingceliang"
                                               textFont:FONT(30)
                                                   text:STRING(@"changjingceliang")
                                                 margin:60];
    midView.layer.cornerRadius = 5;
    [self.view addSubview:midView];
    midView.tag = 2;
    [midView addTarget:self action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];

}



#pragma mark - 辅助
// 创建垂直排版的视图（可点击）
- (UIControl *)createVerticalModeView:(CGRect)frame
                            imageSize:(CGSize)size
                            imageName:(NSString *)name
                             textFont:(UIFont *)font
                                 text:(NSString *)text
                               margin:(CGFloat)margin {
    
    // 1.创建基底视图
    UIControl *basalView = [[UIControl alloc] initWithFrame:frame];
    basalView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.7];

    // 2.创建图片
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
    imageView.image = [UIImage imageNamed:name];
    [basalView addSubview:imageView];
    
    // 3.创建文字
    CGFloat fontSize = [font pointSize];
    CGFloat width = basalView.width;
    CGFloat height = [text getSizeOfString:fontSize].height;
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, height)];
    textLabel.adjustsFontSizeToFitWidth = YES;
    textLabel.font = font;
    textLabel.text = text;
    textLabel.textColor = CJ_THEME_COLOR;
    textLabel.textAlignment = NSTextAlignmentCenter;
    [basalView addSubview:textLabel];
    
    // 4.适配距离
    CGFloat remine = basalView.height - imageView.height - textLabel.height - margin;
    imageView.top = remine / 2;
    imageView.x = basalView.width / 2;
    textLabel.top = imageView.bottom + margin;
    textLabel.x = basalView.width / 2;

    return basalView;

}

// 创建水平排版的视图（可点击）
- (UIControl *)createHorizontalModeView:(CGRect)frame
                              imageSize:(CGSize)size
                              imageName:(NSString *)name
                               textFont:(UIFont *)font
                                   text:(NSString *)text
                                 margin:(CGFloat)margin {
    
    // 1.创建基底视图
    UIControl *basalView = [[UIControl alloc] initWithFrame:frame];
    basalView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.7];

    // 2.创建图片
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
    imageView.image = [UIImage imageNamed:name];
    [basalView addSubview:imageView];

    // 3.创建文字
    CGFloat fontSize = [font pointSize];
    CGFloat width = [text getSizeOfString:fontSize].width + 10; // 防止iOS10以上不适配
    CGFloat height = basalView.height;
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, height)];
    textLabel.adjustsFontSizeToFitWidth = YES;
    textLabel.font = font;
    textLabel.text = text;
    textLabel.textColor = CJ_THEME_COLOR;
    textLabel.numberOfLines = 2;
    textLabel.textAlignment = NSTextAlignmentCenter;
    [basalView addSubview:textLabel];
    
    // 4.适配距离
    CGFloat remine = basalView.width - imageView.width - textLabel.width - margin;
    while (remine < 0) {
        textLabel.width /= 2;
        remine = basalView.width - imageView.width - textLabel.width - margin;
    }
    
    imageView.left = remine / 2;
    imageView.y = basalView.height / 2;
    textLabel.left = imageView.right + margin;
    textLabel.y = basalView.height / 2;

    return basalView;
}



#pragma mark - 点击事件
- (void)leftItemsAction:(NSInteger)leftIndex {
    
    ByLogTestVC *vc = [[ByLogTestVC alloc] init];
    [self by_normalPush:vc hasNavi:YES];
}
- (void)rightItemsAction:(NSInteger)rightIndex {
    
    if ([ByBlueToothHandleModel shareInstance].characteristic) {
        [UIAlertView showWithTitle:STRING(@"jinru") message:STRING(@"jinrulanyajiemian") cancelButtonTitle:STRING(@"quxiao") otherButtonTitles:@[STRING(@"queding")] tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
            if (buttonIndex == 0) {
            } else {
                ByBlueToothListVC *vc = [[ByBlueToothListVC alloc] init];
                [self by_normalPush:vc hasNavi:YES];
            }
        }];
    } else {
        ByBlueToothListVC *vc = [[ByBlueToothListVC alloc] init];
        [self by_normalPush:vc hasNavi:YES];

    }
    

    
}
- (void)tapAction:(UIControl *)tapview {
    
    switch (tapview.tag) {
        case 1:
        {
            break;
        }
        case 2:             // 场景测量
        {
            ByUISceneVC *vc = [[ByUISceneVC alloc] init];
            [self by_normalPush:vc hasNavi:YES];
            break;
        }
        case 3:             // 实时测量
        {
            ByUIRealTimeVC *vc = [[ByUIRealTimeVC alloc] init];
            [self by_normalPush:vc hasNavi:YES];
            break;
        }
        case 4:
        {
            ByUIDataImportVC *vc = [[ByUIDataImportVC alloc] init];
            [self by_normalPush:vc hasNavi:YES];
            break;
        }
        case 5:             // 设置
        {
            ByUISettingVC *vc = [[ByUISettingVC alloc] init];
            [self by_normalPush:vc hasNavi:YES];
            break;
        }
        case 6:             // 帮助文档
        {
            ByUIHelpVC *vc = [[ByUIHelpVC alloc] init];
            [self by_normalPush:vc hasNavi:YES];
            break;
        }
        default:
            break;
    }
}


@end
